<template>
	<div class="consult w944">
		<alert ref="alert"></alert>
		<deleteAlert ref="deleteAlert"></deleteAlert>
		<div class="label_top">
			<button @click="reBack" v-if="this.nowTypeCd=='addTj'"><i><&nbsp;</i>返回</button>
			<p><i class="line" v-if="this.nowTypeCd!='addTj'"></i>{{updateTitle}}</p>
			<b class="rightBtn" @click="tuijianBtn">推荐管理</b>
		</div>
		<div class="label_table">
			<div class="el-tabs-report">
				<a class="listOne" @click="createLecture" v-show="this.nowTypeCd==''?true:false">新增活动讲座</a>
				<a class="listOne" @click="addSave" v-show="this.nowTypeCd=='addTj'?true:false">保存</a>
				<a class="listTwo" @click="importDaoChu">导出数据</a>
				<div class="msearch">
					<input placeholder="请输入标题关键词……" v-model="search"/>
					<a @click="getTableList"><img src="../../assets/qudaoImg/search.png"/></a>
				</div>
			</div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55"></el-table-column>
				<el-table-column prop="title" label="活动讲座标题"></el-table-column>
				<el-table-column label="活动时间">
					<template slot-scope="scope">{{scope.row.starttime}} - {{scope.row.endtime}}</template>
				</el-table-column>
				<el-table-column label="状态" width="80">
					<template slot-scope="scope">
						<span v-if="scope.row.status==0">待发布</span>
						<span v-else-if="scope.row.status ==1">预约中</span>
						<span v-else-if="scope.row.status ==2">进行中</span>
						<span v-else-if="scope.row.status ==3">已结束</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="160">
					<template slot-scope="scope">
						<div class="status" v-if="scope.row.status == 0">
							<el-button @click="handleRelease(scope.row)">发布</el-button>
							<el-button @click="handleEdit(scope.row)" class="blue">编辑</el-button>
							<el-button @click="handleDelete(scope.row)" class="red">删除</el-button>
						</div>
						<div class="status" v-if="scope.row.status == 1">
							<el-button @click="handleEdit(scope.row)">编辑</el-button>
							<el-button @click="handleDelete(scope.row)" class="blue">删除</el-button>
							<el-button @click="handleSignIn(scope.row)" class="red">报名管理</el-button>
						</div>
						<div class="status" v-if="scope.row.status == 2 || scope.row.status == 3">
							<el-button @click="handleSeen(scope.row)">查看</el-button>
							<el-button @click="handleSignIn(scope.row)" class="red">报名管理</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<el-button @click="toggleSelection(tableData)" class="allSelect">全选</el-button>
				<el-button @click="handleDeleteAll" class="allDelete">批量删除</el-button>
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	export default {
		data() {
			return {
				lecture:'',
				updateTitle:'活动讲座',
				search:'',
				tableData:[],
				multipleSelection: [],
		        pagesize: 15,
		        currentpage: 1,
		        total: 1,
				nowTypeCd:'',
				deleteType:'delete'
			}
		},
		methods: {
			getParams() {
				let that = this;
				var routerParams = this.$route.params.type;
				if(routerParams == 'addTj') {
					that.nowTypeCd = 'addTj';
					that.updateTitle = '推荐活动讲座';
				}else{
					that.nowTypeCd = '';
				}
			},
			getTableList(){
				this.$ajax({
					url: this.$root.jifuUrl + "/getActivityLecturePage?"+'page='+this.currentpage+'&num='+this.pagesize+'&name='+this.search+'&rootcompanyid='+1,
					method: 'GET',
					data: {},
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
						}
					}
				});
			},
			handleRelease(row){
				this.transitionRow = row;
				this.$rootFunction().rootRelease();
			},
			fabuSubmit(){
				var s={id:this.transitionRow.id};
				this.$ajax({ 
					url: this.$root.jifuUrl + "/releaseActivityLecture",
					method: 'POST',
					data: this.qs.stringify(s),
					success: res => {
						if (res.data.code == 200) {
							this.$rootFunction().releaseResSuccess();
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();
						}
					}
				});
			},
			handleEdit(row) {
				this.$ajax({
					url: this.$root.jifuUrl + "/getActivityLectureById?" + 'id=' + row.id,
					method: 'GET',
					success: res => {
						if(res.data.code == 200) {
							this.$router.push({
								name: 'addlecture',
								params: {
									type: 'edit'
								}
							})
							this.$root.lectureMsg = res.data.data;
						}else{
							console.log("获取详细信息失败")
						}
					}
				});
			},
			handleSeen(row){
				this.$ajax({
					url: this.$root.jifuUrl + "/getActivityLectureById?" + 'id=' + row.id,
					method: 'GET',
					success: res => {
						if(res.data.code == 200) {
							this.$router.push({
								name: 'addlecture',
								params: {
									type: 'seen'
								}
							})
							this.$root.lectureMsg = res.data.data;
						}else{
							console.log("获取详细信息失败")
						}
					}
				});
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					let s={id:this.transitionRow.id};
					this.$ajax({ 
						url: this.$root.jifuUrl + "/deleteActivityLectureById",
						method: 'POST',
						data: this.qs.stringify(s),
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					let s={ids:this.multipleSelection.join(',')};
					this.$ajax({ 
						url: this.$root.jifuUrl + "/deleteActivityLectureByIds",
						data:this.qs.stringify(s),
						method: 'POST',
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			addSave(){
				let s={ids:this.multipleSelection.join(',')};
				this.$ajax({
					url: this.$root.jifuUrl + "/addRecommendActivityLecture",
					method: 'POST',
					data: this.qs.stringify(s),
					success: res => {
						if(res.data.code == 200) {
							this.$refs.alert.show({alertText: '保存成功'});
							setTimeout(() =>{
								this.$router.push({
									name:'addconsult'
								});
							},2000)
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();
						}
					}
				})
			},
			handleSignIn(row){
				this.$router.push({
					name: 'signIn',
					params: {
						lectureId: row.id,
						lectureName:row.title
					}
				})
			},
			tuijianBtn(){
				this.$router.push({
					name: 'addconsult'
				})
			},
			createLecture() {
				this.$router.push({
					path: '/addlecture'
				})
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				val.forEach(row => {
					this.multipleSelection.push(row.id);
				})
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			reBack(){
				this.$router.go(-1);
			},
			importDaoChu(){}
		},
		components: {
			alert,
			deleteAlert
		},
		mounted: function() {
			this.getParams();
			this.getTableList();
			this.lecture = this.$root.huasan.lecture;
		}
	}
</script>